package com.gla.service.impl;

import com.gla.common.Const;
import com.gla.common.ResponseCode;
import com.gla.dao.UserMapper;
import com.gla.domain.User;
import com.gla.domain.UserVO;
import com.gla.service.IUserService;
import com.gla.util.ServerResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService implements IUserService {
    @Autowired
    UserMapper userMapper;

    /**
     * 转换为前端显示的VO类
     * */
    private UserVO convert(User user){
        UserVO userVO = new UserVO();
        userVO.setId(user.getId());
        userVO.setAccount(user.getAccount());
        userVO.setName(user.getName());
        userVO.setPhoto(user.getPhoto());
        userVO.setIs_vip(user.getIs_vip());
        return userVO;
    }

    /**
     * 修改个人信息
     * */
    @Override
    public ServerResponse updateUserLogic(User user) {
        int count = userMapper.updatePrimaryKey(user);
        if(count == 0){
            //修改失败
            return ServerResponse.createServerResponseByFail(ResponseCode.USERINFO_UPDATE_FAIL.getCode(),ResponseCode.USERINFO_UPDATE_FAIL.getMsg());
        }
        //根据id查询
        User newuser = userMapper.selectByPrimaryKey(user.getId());
        UserVO userVO = convert(newuser);
        return ServerResponse.createServerResponseBySuccess(userVO);
    }

    /**
     * 修改个人用户名
     * */
    @Override
    public ServerResponse updateUserName(User user) {
        int count = userMapper.updateName(user);
        if(count == 0){
            //修改失败
            return ServerResponse.createServerResponseByFail(ResponseCode.USERINFO_UPDATE_FAIL.getCode(),ResponseCode.USERINFO_UPDATE_FAIL.getMsg());
        }
        //根据id查询
        User newuser = userMapper.selectByPrimaryKey(user.getId());
        UserVO userVO = convert(newuser);
        return ServerResponse.createServerResponseBySuccess(userVO);
    }

}
